package com.snow.campuslink.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.snow.campuslink.project.result.Result;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 顶层的controller
 *
 *
 * @author lianhong
 * @date 2020-8-24
 */
public class BaseRestController<T extends IService, E> {
    @Resource
    protected T baseService;

    /**
     * 获取搜索用的QueryWrapper(查询条件，需要自己去重载,不然就只有搜索全部)
     * @param searchValue 搜索的值
     * @param e 需要精确搜索的参数
     * @return
     */
    protected QueryWrapper<E> getSearchQueryWrapper(String searchValue,E e){
        return new QueryWrapper();
    }



//    /**
//     * 模糊搜索
//     * @param page
//     * @param size
//     * @param searchValue 需要模糊搜索的值
//     * @param e 需要精确搜索的参数
//     * @return
//     */
//    @GetMapping("/search")
//    public Result fuzzySearch(Integer page, Integer size,String searchValue,E e){
//        Assert.notNull(page, "页数不能为空");
//        Assert.notNull(size, "页大小不能为空");
//        IPage pageInfo = baseService.page(new Page(page, size), getSearchQueryWrapper(searchValue,e));
//        return Result.success().data(pageInfo);
//    }

    /**
     * 分页获取
     *
     * @return
     */
    @GetMapping("/page")
    public Result list(Integer page, Integer size) {
        Assert.notNull(page, "页数不能为空");
        Assert.notNull(size, "页大小不能为空");
        IPage pageInfo = baseService.page(new Page(page, size), null);
        return Result.success().data(pageInfo);
    }


    /**
     * 列表
     */
    @PostMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        QueryWrapper<E> eQueryWrapper = new QueryWrapper<>();
        eQueryWrapper.allEq(params);
        return Result.success().data(baseService.list(eQueryWrapper));
    }


    /**
     * 根据主键查询实体类信息
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id")Long id){
        return Result.success().data(baseService.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody E e){
        baseService.save(e);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody E e){
        baseService.updateById(e);

        return Result.success();
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        baseService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatchMapping(@RequestBody Long[] ids) {
        Assert.notNull(ids, "参数不能为空");
        boolean res = baseService.removeById(ids);
        if (res) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}

